<template>
  <div :style="{ height: winHeight, width: winWidth }">
    <div class="index">
      <div class="indexTop">
        <h1>岳西掌上公交</h1>
        <div class="indexTopNotice">
          <span v-if="cur" class="msgPoint"></span>
          <p>公告</p>
        </div>
      </div>
      <div class="ad border-top">
        <div class="notice flexStart">
          <van-notice-bar
            color="#1989fa"
            background="#ecf9ff"
            left-icon="volume"
          >
          {{notice}}
          </van-notice-bar>
        </div>
      </div>
      <div class="searchInput flexStart">
        <div class="chooseCont">
          <van-dropdown-menu>
            <van-dropdown-item v-model="value1" :options="option1" />
          </van-dropdown-menu>
        </div>
        <div class="chooseInput">
          <van-search placeholder="请输入搜索关键词" v-model="value" />
        </div>
      </div>
      
    </div>
    <div class="amap-wrapper">
      <el-amap class="amap-box" :vid="'amap-vue'" :center="center" :zoom="zoom" :visible="visible"></el-amap>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import { Search, Icon, NoticeBar, DropdownMenu, DropdownItem } from 'vant'
  Vue.use(Search)
  Vue.use(Icon);
  Vue.use(NoticeBar);
  Vue.use(DropdownMenu).use(DropdownItem);

  // import AMap from 'AMap'
  export default {
    name: 'Home',
    components: {
      [ Search.name ]: Search
    },
    data () {
      let self = this;
      return {
        notice: 
          '岳西公交岳西公交岳西公交岳西公交岳西公交岳西公交岳西公交岳西公交岳西公交1',
        cur: true,
        winWidth: '',
        winHeight: '',
        zoom: 16,
        center: [116.35631,30.86094],
        visible: true,
        lng: 0,
        lat: 0,
        loaded: false,
        value1: 0,
      option1: [
        { text: '查公交', value: 0 },
        { text: '查站点', value: 1 }
      ]
      }
    },
    mounted(){
      this.getWinWidth()
      this.qwe()
    },
    created(){
      setInterval(this.scroll, 1000)
    },
    methods: {
      getWinWidth: function() {
        this.winWidth = document.body.clientWidth
        this.winHeight= document.body.clientHeight
      },
      qwe(){
        let that = this
        var map = new AMap.Map('container', {
            resizeEnable: true
        });
        AMap.plugin('AMap.Geolocation', function() {
            var geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：5s
                buttonPosition:'RB',    //定位按钮的停靠位置
                buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点

            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition(function(status,result){
                if(status=='complete'){
                    that.onComplete(result)
                }else{
                    that.onError(result)
                }
            });
        });
      },
      //解析定位结果
      onComplete(data) {
          // document.getElementById('status').innerHTML='定位成功'
          var str = [];
          str.push('定位结果：' + data.position);
          str.push('定位类别：' + data.location_type);
          if(data.accuracy){
              str.push('精度：' + data.accuracy + ' 米');
          }//如为IP精确定位结果则没有精度信息
          str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
          let f = data.position

          this.center = [data.position.lng, data.position.lat];

          // document.getElementById('result').innerHTML = str.join('<br>');
      },
      //解析定位错误信息
      onError(data) {
        console.log('定位失败')
        console.log(data.message)
        alert('定位失败'+data.message)
          // document.getElementById('status').innerHTML='定位失败'
          // document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
      },
    }
  }
</script>
<style lang='stylus' scoped>
@import '../assets/common.styl'
  .index
    position absolute
    left: 0
    top: 0
    right: 0
    background-color: #fff
    z-index: 999
    padding-bottom: .1rem
    .indexTop
      text-align center
      &
        h1
          font-size: .3rem
          color: $color
          height: .8rem
          line-height: .8rem
      .noticeIcon
        font-size: .28rem
        color: $color
        margin-right: .1rem
      .noticeCont
        width 5rem
        margin-left: .2rem
        position: relative
        height: .4rem
        overflow: hidden
      .noticeCont
        ul
          position: absolute
          left: 0
          top: 0
          height: .4rem
          .noticeDetail
            width 5rem
            ellipsis()
            line-height: .4rem
    .notice
      div
        width: 100%
    .searchInput
      position: absolute
      top: 3rem
      left: 0
      right: 0
      background-color: #fff
      padding-right: .1rem
      .chooseCont
        width: 1rem
        flex: 0 0 2rem
      .chooseInput
        flex: 1
      .chooseInput 
        .van-search
          padding: 0
    .indexTopNotice
      position: absolute
      width: 1rem
      right: 0
      top: 0
      height: .8rem
      line-height: .8rem
      text-align center
      .msgPoint
        position: absolute
        width: .1rem
        height: .1rem
        border-radius: 100%
        background-color: red
        right: .21rem
        top: .21rem
        z-index: 99999
  .amap-wrapper
    position: fixed
    left: 0
    right: 0
    top: 0
    bottom: 0
</style>
